//
//  BannerCell.m
//  ForYourBeauty
//
//  Created by Kelly xu on 2019/8/18.
//  Copyright © 2019 changrenTech. All rights reserved.
//

#import "BannerCell.h"

@implementation BannerCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)setCellValue:(NSMutableArray*)bannerList{
    for (UIView *view in self.scrollView.subviews) {
        [view removeAllSubviews];
    }
    [self.bannerList removeAllObjects];
    [self.bannerList addObjectsFromArray:bannerList];
    for (int i=0; i<[bannerList count]; i++) {
        BannerListModel *model = [bannerList objectAtIndex:i];
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(self.scrollView.width*i, 0, self.scrollView.width, self.scrollView.height)];
        [image sd_setImageWithURL:[NSURL URLWithString:model.bannerImg]];
        image.tag = 9000+i;
        image.userInteractionEnabled = YES;
        [self.scrollView addSubview:image];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]init];
        [tapGesture addTarget:self action:@selector(tapBanner:)];
        [image addGestureRecognizer:tapGesture];
        
        UIImageView *bar = [[UIImageView alloc]initWithFrame:CGRectMake(0, image.height-20, image.width, 20)];
        bar.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        [image addSubview:bar];
        
        UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, image.width-20, 20)];
        title.font = [UIFont systemFontOfSize:13];
        title.textColor = [UIColor whiteColor];
        title.textAlignment = NSTextAlignmentRight;
        title.text = [NSString stringWithFormat:@"%@",model.bannerTitle];
        [bar addSubview:title];
    }
    if ([self.bannerList count]) {
        if (!self.hasTimer) {
            self.hasTimer = YES;
            [self addTimer];
        }
    }
}
-(void)tapBanner:(UITapGestureRecognizer*)gesture{
    NSInteger tag = gesture.view.tag - 9000;
    BannerListModel *model = [self.bannerList objectAtIndex:tag];
    if ([self.delegate respondsToSelector:@selector(bannerClicked:)]) {
        [self.delegate bannerClicked:model];
    }
}
- (void)addTimer {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
-(void)nextImage{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self.scrollView.contentOffset.x == ([self.bannerList count]-1)*self.scrollView.width) {
            [self.scrollView setContentOffset:CGPointZero animated:YES];
        }else{
            [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x+self.scrollView.width,0) animated:YES];
        }
    });
}
-(NSMutableArray*)bannerList{
    if (!_bannerList) {
        _bannerList = [[NSMutableArray alloc] init];
    }
    return _bannerList;
}
@end
